/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller.CoreServlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Controller.CoreServlet.Ultility.*;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.text.*;
import Model.ObjectBean.ViewBean.*;
import javax.servlet.RequestDispatcher;

/**
 *
 * @author Tran Nguyen Anh Tu
 */
public class CartServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        

        
       
        
        // -- Lấy itemID mà user chọn mua
        String itemID= request.getParameter("resourceID");
        String item_ID=request.getParameter("itemID");
        String delete_ID=request.getParameter("delete");
        //out.println(item_ID);
        out.println(delete_ID);
        String numberOrder=request.getParameter("numberUpdate");
        //out.println(numberOrder);
        HttpSession session = request.getSession();
        ShoppingCart cart;
        synchronized (session){
            cart=(ShoppingCart)request.getSession().getAttribute("shoppingcart");
          // New customer will create a new cart 
          // Previous customer will still use their exist cart
            if(cart==null)
            {
                cart=new ShoppingCart();
                session.setAttribute("shoppingcart", cart);
            }
            if(itemID!=null || item_ID!=null || delete_ID!=null)
            {
                if(numberOrder==null && delete_ID==null)
                {
                    cart.addItem(itemID);
                    RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/MainJSP/WarningPage.jsp");
                    dispatcher.forward(request, response);
                    //ut.println("Thêm");

                }
                if(numberOrder != null && delete_ID==null)
                {
                    int num=Integer.parseInt(numberOrder);
                    cart.setNumOrdered(item_ID,num);
                    RequestDispatcher dispatcher2 = request.getRequestDispatcher("/WEB-INF/MainJSP/CartPage.jsp");
                    dispatcher2.forward(request, response);
                    //out.println("Cập nhật");
                }
                if(delete_ID!=null)
                {
                    for(int i=0;i<cart.getItemsOrdered().size();i++)
                    {
                        ItemOrder order = (ItemOrder)cart.getItemsOrdered().get(i);
                        if(order.getItem_ID().equals(delete_ID))
                            
                        {
                            cart.getItemsOrdered().remove(i);
                            RequestDispatcher dispatcher3 = request.getRequestDispatcher("/WEB-INF/MainJSP/CartPage.jsp");
                            dispatcher3.forward(request, response);
                        }
                    }
                        
                }
                    
            }
            
        }
        
       /* synchronized (session){
            List ItemsOrdered = cart.getItemsOrdered();
            if(ItemsOrdered.size()==0)
                out.println("Giỏ hàng của bạn rỗng");
            else
                for(int i=0;i<ItemsOrdered.size();i++)
                {
                    ItemOrder order = (ItemOrder)ItemsOrdered.get(i);
                    out.println(order.getItem_ID());
                    out.println(order.getCoverImage());
                    out.println(order.getLabel());
                    out.println(order.getUnitCost());
          
                }
        }*/
        
        
        
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
